package com.yztc.yidoutang.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.yztc.yidoutang.R;
import com.yztc.yidoutang.bean.SharingsBean;

import java.util.List;

/**
 * Created by My on 2016/10/20.
 */

public class DanPinAdapter extends RecyclerView.Adapter<DanPinAdapter.MyViewHolder> {
    private List<SharingsBean> list;
    private Context context;
    private OnItemClickListener listener;

    public void setListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    public DanPinAdapter(List<SharingsBean> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        MyViewHolder holder = new MyViewHolder(LayoutInflater.from(context)
                .inflate(R.layout.item_danpin, parent, false));
        return holder;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {
        Glide.with(context).load(list.get(position).getNormalImage()).into(holder.image);
        holder.title.setText(list.get(position).getTitle());
        holder.price.setText("¥"+list.get(position).getPrice());
        holder.like.setChecked(list.get(position).isIsLike());
        if (listener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos = holder.getLayoutPosition();
                    listener.onItemClick(holder.itemView, pos);
                }
            });

            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    int pos = holder.getLayoutPosition();
                    listener.onItemLongClick(holder.itemView, pos);
                    return false;
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        ImageView image;
        TextView title;
        TextView price;
        CheckBox like;

        public MyViewHolder(View itemView) {
            super(itemView);
            image= (ImageView) itemView.findViewById(R.id.dp_image);
            title= (TextView) itemView.findViewById(R.id.dp_title);
            price= (TextView) itemView.findViewById(R.id.dp_price);
            like= (CheckBox) itemView.findViewById(R.id.dp_like);
        }
    }
}

